//
//  XLSearchBar.m
//  Current
//
//  Created by 于鹏 on 2017/8/14.
//  Copyright © 2017年 YIVIEW. All rights reserved.
//

#import "XLSearchBar.h"

@interface XLSearchBar ()
@property (nonatomic, assign) BOOL isSearch;
@end

@implementation XLSearchBar

- (void)awakeFromNib {
    [super awakeFromNib];
    [self config];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self config];
        
    }
    return self;
}

- (void)config {
    self.searchBarStyle = XLSearchBarStyleMinimal;
    self.textBordStyle = XLTextBorderStyleNone;
    self.placeholder = @"搜索";
    self.delegate = self;
}

#pragma mark - UITextFeildDelegate

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    return YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    self.isSearch = YES;
    if (self.searchBlock) {
        self.searchBlock(searchBar.text);
    }
    if (self.searchDelegate && [self.searchDelegate respondsToSelector:@selector(xl_searchBar:searchText:)]) {
        [self.searchDelegate xl_searchBar:self searchText:searchBar.text];
    }
    [searchBar resignFirstResponder];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if (searchText.length > 0) {
        if (self.searchBlock) {
            self.searchBlock(searchText);
        }
        
        if (self.searchDelegate && [self.searchDelegate respondsToSelector:@selector(xl_searchBar:searchText:)]) {
            [self.searchDelegate xl_searchBar:self searchText:searchText];
        }
    }
    
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [self xl_resignFirst];
    searchBar.text = @"";
    [searchBar setShowsCancelButton:NO animated:YES];
    if (self.searchDelegate && [self.searchDelegate respondsToSelector:@selector(xl_cancleSearch:)]) {
        [self.searchDelegate xl_cancleSearch:self];
    }
}

// 重新设置 searchbar cancel按钮 为搜索
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
    
    // 修改UISearchBar右侧的取消按钮文字颜色及背景图片
    for (UIView *searchbuttons in [[searchBar subviews][0] subviews]){
        if ([searchbuttons isKindOfClass:[UIButton class]]) {
            UIButton *cancelButton = (UIButton*)searchbuttons;
            [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
            // 修改文字颜色
            [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
            
            // 修改按钮背景
            //            [cancelButton setBackgroundImage:nil forState:UIControlStateHighlighted];
        }
    }
}

#pragma mark - Setter

- (void)setXlSearchBarStyle:(XLSearchBarStyle)xlSearchBarStyle {
    _xlSearchBarStyle = xlSearchBarStyle;
    UISearchBarStyle style = (xlSearchBarStyle == XLSearchBarStyleDefault) ? UISearchBarStyleDefault : ((xlSearchBarStyle == XLSearchBarStyleMinimal) ? UISearchBarStyleMinimal : UISearchBarStyleProminent);
    self.searchBarStyle = style;
}

- (void)setTextBordStyle:(XLTextBorderStyle)textBordStyle {
    _textBordStyle = textBordStyle;
    UITextBorderStyle style = (textBordStyle == XLTextBorderStyleNone) ? UITextBorderStyleNone : ((textBordStyle == XLTextBorderStyleLine) ? UITextBorderStyleLine : ((textBordStyle == XLTextBorderStyleBezel) ? UITextBorderStyleBezel : UITextBorderStyleRoundedRect));
    [self searchBarTextFeild].borderStyle = style;
}
- (void)setPlaceholderText:(NSString *)placeholderText {
    _placeholderText = placeholderText;
    self.placeholder = placeholderText;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
    [[self searchBarTextFeild] setValue:placeholderColor forKey:@"_placeholderLabel.textColor"];
}

- (void)setBarBackgroudColor:(UIColor *)barBackgroudColor {
    _barBackgroudColor = barBackgroudColor;
    self.barTintColor = barBackgroudColor;
}

- (void)setTextBackgroudColor:(UIColor *)textBackgroudColor {
    _textBackgroudColor = textBackgroudColor;
    [[[self.subviews firstObject]subviews]lastObject].backgroundColor = textBackgroudColor;
}

- (void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    [self searchBarTextFeild].textColor = textColor;
}

- (void)setSearchTintColor:(UIColor *)searchTintColor {
    _searchTintColor = searchTintColor;
    self.tintColor = searchTintColor;
}

- (void)setPlaceholderFontSize:(CGFloat)placeholderFontSize {
    _placeholderFontSize = placeholderFontSize;
     [[self searchBarTextFeild] setValue:[UIFont systemFontOfSize:placeholderFontSize] forKeyPath:@"_placeholderLabel.font"];
}

- (void)setTextFontSize:(CGFloat)textFontSize {
    _textFontSize = textFontSize;
    [self searchBarTextFeild].font = [UIFont systemFontOfSize:textFontSize];
}

#pragma mark - Private Method 
//获取输入框
- (UITextField *)searchBarTextFeild {
    UITextField * texFeild = nil;
    for (UIView* subview in [[self.subviews lastObject] subviews]) {
        
        if ([subview isKindOfClass:[UITextField class]]) {
            texFeild = (UITextField*)subview;
            
        } else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
        {
            [subview removeFromSuperview];
        }
    }
    return texFeild;
}

#pragma mark - Public Method
- (void)xl_becomeFirst {
    [self becomeFirstResponder];
}

- (void)xl_resignFirst {
    self.isSearch = NO;
    [self resignFirstResponder];
}

- (void)searchBarTextSearchTextBlock:(XLSearchTextBlock)block {
    self.searchBlock = block;
}
@end
